sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/model/json/JSONModel",
    "sap/m/bbf41/Module/Formatter"
], function (Controller, JSONModel, Formatter) {
    "use strict";
    var oJSONModel;
    return Controller.extend("sap.m.bbf41.Module.Controller", {
        onInit: function () {
            oJSONModel = this.loadFoundryDataModel();
            var oView = this.getView();
            oView.setModel(oJSONModel);

            //var oTable = oView.byId('table');
            //oTable.onAfterRendering = function () {
            //    if (sap.ui.table.Table.prototype.onAfterRendering) {
            //        sap.ui.table.Table.prototype.onAfterRendering.apply(this, arguments);
            //    }
            //    var cols = this.getColumns();
            //
            //    for (var i = 0; i < cols.length; i++) {
            //        if (cols[i].getColoredStyleClass) {
            //            var coloredCol = cols[i].getColoredStyleClass();
            //            if (coloredCol) {
            //                var th = this.$().find('.sapUiTableColHdr').find('.sapUiTableCol');
            //                $(th[i]).addClass(coloredCol);
            //
            //                var rows = this.$().find('.sapUiTableTr');
            //
            //                for (var j = 0; j < rows.length; j++) {
            //                    var r = rows[j];
            //                    var tds = $(r).find('td');
            //                    $(tds[i]).addClass(coloredCol);
            //                }
            //            }
            //        }
            //    }
            //}
        },
        Formatter: Formatter,
        handlePressOpenMenu: function (oEvent) {
            var oButton = oEvent.getSource();
            // create menu only once
            if (!this._menu) {
                this._menu = sap.ui.xmlfragment(
                    "sap.m.bbf41.Module.MenuItemEventing",
                    this
                );
                this.getView().addDependent(this._menu);
            }
            var eDock = sap.ui.core.Popup.Dock;
            this._menu.open(this._bKeyboard, oButton, eDock.BeginTop, eDock.BeginBottom, oButton);
        },
        handleRefresh: function() {
            oJSONModel = this.loadFoundryDataModel();
            var oView = this.getView();
            oView.setModel(oJSONModel);
        },
        loadFoundryDataModel: function () {
            var me = this,
                oModel = new JSONModel(),
                oView = this.getView(),
                oTable = oView.byId('table');
            oTable.setBusy(true);
            jQuery.ajax({
                url: '/NEP/ALS/api/rtb2',
                dataType: "json",
                success: function (resp) {
                    var oData = me.buildTableData(resp);
                    oModel.setData(oData);
                }.bind(this),
                error: function () {
                    jQuery.sap.log.error("failed to load json");
                },
                complete: function() {
                    oTable.setBusy(false);
                }
            });
            return oModel;
        },
        onNavButtonPress: function () {
            window.history.back();
        },
        handleMenuItemPress: function (oEvent) {
        },
        buildTableData: function () {
            var data = {
                "Collection": [
                    {
                        "Type": "Line Q",
                        "Target": 983,
                        "Current": 621,
                        "Forecast": 955,
                        "Planned": 643
                    },
                    {
                        "Type": "Line P",
                        "Target": 450,
                        "Current": 145,
                        "Forecast": 482,
                        "Planned": 135
                    }
                ]
            };
            return data;
        }
    });

});
